package com.alex.books;

import java.io.File;
import java.util.Vector;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;

import com.alex.database.BookDao;
import com.alex.exception.UnsupportedFormatException;
import com.alex.formates.BookFormat;
import com.alex.formates.ReaderFormat;
import com.alex.formates.ReaderPlugin;
import com.alex.formates.epub.EPUBPlugin;
import com.alex.formates.qd.QDPlugin3;
import com.alex.formates.txt.TXTPlugin2;
import com.alex.formates.txt.TXTPlugin3;
import com.alex.formates.umd.UMDPlugin;
import com.alex.listener.BookListener;

/**
 * 
 * @author Alex.Lu
 *
 */
public class BookPageFactory {
	
	
	/**
	 * 阅读各种不同格式的插件
	 */
	private ReaderPlugin mReaderPlugin;
	/**
	 * 阅读格式
	 */
	private ReaderFormat mReaderFormat;
	
	private BookListener mBookListener;
	
	private static BookPageFactory instance;
	
	public static BookPageFactory getInstance(){
		if(instance == null){
			instance = new BookPageFactory();
		}
		return instance;
	}
	
	private BookPageFactory(){
		
	}
	
	/**
	 * 打开数据
	 * @param mBook
	 * @param mode
	 * @return
	 * @throws UnsupportedFormatException 
	 */
	public boolean openBook(Context context,BaseBook mBook) throws UnsupportedFormatException{
		
		if(mBook.mIsFirstReader == 1){
			
			generatedCatalog(mBook,0);
			
			//将数据插入数据库
			BookDao.getInstance().insertChapter(context, mBook.mChapters);
			
			mBook.mReaderingInfo.mIndex = 0;
			mBook.mReaderingInfo.mMarkPos = 0;
			mBook.mReaderingInfo.mCurChapter = mBook.mChapters.get(0);
			BookStore.getInstance().setHasRead(context, mBook);
			
		}else{
			File file = new File(mBook.mBookPath);
			String name = file.isDirectory() ? file.getPath() : file.getName();
			int index = name.lastIndexOf(".");
			String extension = (index != -1) ? name.substring(index+1).toLowerCase() : "" ;
			if(extension.equals(BookFormat.FORMAT_TXT) || extension.equals(BookFormat.FORMAT_SBN)){
				mReaderPlugin = new TXTPlugin3();
			}else if(extension.equals(BookFormat.FORMAT_EPUB)){
				mReaderPlugin = new EPUBPlugin();
			}else if(extension.equals(BookFormat.FORMAT_UMD)){
				mReaderPlugin = new UMDPlugin();
			}else if(extension.equals(BookFormat.FORMAT_QD)){
				mReaderPlugin = new QDPlugin3();
			}else
			{
				//不支持该格式
				throw new UnsupportedFormatException(extension);
			}
			mBook.mChapters = BookStore.getInstance().getChapters(context,mBook.mBookId);
			mReaderPlugin.decode(mBook,file);
		}
		
		mReaderPlugin.setBook(mBook);
		
		if(mBookListener!=null){
			mBookListener.onFinishLoadBook();
		}
		
		return true;
	}
	
	public boolean geteratedCatalog2(Context context, BaseBook mBook,String keywords){
		
		File file = new File(mBook.mBookPath);
		String name = file.isDirectory() ? file.getPath() : file.getName();
		int index = name.lastIndexOf(".");
		TXTPlugin3 tex = new TXTPlugin3();
		tex.generatedCatalog2(mBook, file,keywords);
		BookDao.getInstance().deleteChapter(context, mBook.mBookId);
		BookDao.getInstance().insertChapter(context, mBook.mChapters);
		return true;
	}
	
	/**
	 * 生成目录
	 * @param mBook
	 * @param mode
	 * @return
	 * @throws UnsupportedFormatException 
	 */
	private boolean generatedCatalog(BaseBook mBook,int mode) throws UnsupportedFormatException{
		File file = new File(mBook.mBookPath);
		String name = file.isDirectory() ? file.getPath() : file.getName();
		int index = name.lastIndexOf(".");
		String extension = (index != -1) ? name.substring(index+1).toLowerCase() : "" ;
		
		if(extension.equals(BookFormat.FORMAT_TXT) || extension.equals(BookFormat.FORMAT_SBN)){
			mReaderPlugin = new TXTPlugin3();
		}else if(extension.equals(BookFormat.FORMAT_EPUB)){
			mReaderPlugin = new EPUBPlugin();
		}else if(extension.equals(BookFormat.FORMAT_UMD)){
			mReaderPlugin = new UMDPlugin();
		}else if(extension.equals(BookFormat.FORMAT_QD)){
			mReaderPlugin = new QDPlugin3();
		}else {
			//不支持该格式
			throw new UnsupportedFormatException(extension);
		}
		if(mReaderPlugin != null){
			mReaderPlugin.generatedCatalog(mBook,file, mode);
		}
		return true;
	}
	
	public SpannableString nextChapter(){
		String text = mReaderPlugin.nextChapter();
		SpannableString str = new SpannableString(text);
		str.setSpan(new StyleSpan(mReaderFormat.mStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		str.setSpan(new TypefaceSpan(mReaderFormat.mFamilyName), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return str;
	}
	
	public SpannableString previousChapter(){
		String text = mReaderPlugin.previousChapter();
		SpannableString str = new SpannableString(text);
		str.setSpan(new StyleSpan(mReaderFormat.mStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		str.setSpan(new TypefaceSpan(mReaderFormat.mFamilyName), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return str;
	}
	
	public SpannableString nextPage(){
		String text =mReaderPlugin.nextPage();
		SpannableString str = new SpannableString(text);
		str.setSpan(new StyleSpan(mReaderFormat.mStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		str.setSpan(new TypefaceSpan(mReaderFormat.mFamilyName), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return str;
	}
	
	public SpannableString previousPage(){
		String text = mReaderPlugin.previousPage();
		SpannableString str = new SpannableString(text);
		str.setSpan(new StyleSpan(mReaderFormat.mStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		str.setSpan(new TypefaceSpan(mReaderFormat.mFamilyName), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return str;
	}
	
	public SpannableString refreshPage(){
		String text = mReaderPlugin.refreshPage();
		SpannableString str = new SpannableString(text);
		str.setSpan(new StyleSpan(mReaderFormat.mStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		str.setSpan(new TypefaceSpan(mReaderFormat.mFamilyName), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return str;
	}
	
	public boolean isLastPage(){
		return mReaderPlugin.isLastPage();
	}
	
	public boolean isFirstPage(){
		return mReaderPlugin.isFirstPage();
	}
	
	public boolean isLastChapter(){
		return mReaderPlugin.isLastChapter();
	}
	
	public boolean isFirstChapter(){
		return mReaderPlugin.isFirstChapter();
	}
	
	public BaseBook getBook(){
		return mReaderPlugin.getBook(); 
	}
	
	
	public Chapter getCurChapter(){
		return mReaderPlugin.getCurChapter();
	}
	
	public void setReaderFormat(float size,int space,int pageW, int pageH){
		mReaderFormat = new ReaderFormat();
		mReaderFormat.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mReaderFormat.mPaint.setTextAlign(Align.LEFT);
		mReaderFormat.mPaint.setTextSize(size);
		mReaderFormat.mSpace = space;
		mReaderFormat.mPageW = pageW;
		mReaderFormat.mPageH = pageH;
		mReaderFormat.setUp();
		if(mReaderPlugin!=null)
			mReaderPlugin.setReaderFormat(mReaderFormat);
	}
	
	public void setReaderingInfo(ReaderingInfo readeringInfo){
		mReaderPlugin.setReaderingInfo(readeringInfo);
	}
	
	public void setFontSize(float size){
		mReaderFormat.mPaint.setTextSize(size);
		mReaderFormat.setUp();
	}
	
	public void setLineSpace(float space){
		mReaderFormat.mSpace = space;
		mReaderFormat.setUp();
	}
	
	public void setFontFamilly(String familyName,int style){
		mReaderFormat.mFamilyName = familyName;
		mReaderFormat.mStyle = style;
		mReaderFormat.mPaint.setTypeface(Typeface.create(familyName, style));
		mReaderFormat.setUp();
	}
	
	public Vector<String> getLines(){
		return mReaderPlugin.getLines();
	}
	
	public void addBookMark(){
		
	}
	
	public void setDBDir(String path){
		if(path != null){
			File dir = new File(path);
			if(dir.exists()){
				dir.mkdir();
			}
			BookStore.getInstance().DBPath = path;
		}
	}
}
